<template>
    <div>
      <!--    面包屑区域 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 卡片视图区域 -->
      <el-card>

        <!-- 查找行 -->
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrdersList">
              <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
            </el-input>
          </el-col>
        </el-row>

        <!-- 订单列表区域 -->
        <el-table :data="orderList" border>
          <!-- 索引列 -->
          <el-table-column type="index"></el-table-column>

          <el-table-column label="订单编号" prop="order_number"></el-table-column>
          <el-table-column label="订单价格" prop="order_price" width="80px"></el-table-column>
          <el-table-column label="是否付款" prop="pay_status" width="80px">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.pay_status === '0' " type="danger">未付款</el-tag>
              <el-tag v-else-if="scope.row.pay_status === '1' " type="primary">已付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="是否发货" prop="is_send" width="80px"></el-table-column>
          <el-table-column label="下单时间" prop="create_time" width="190px">
            <template slot-scope="scope">
              {{ scope.row.create_time | dataFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template>
              <!-- 修改按钮 -->
              <el-tooltip effect="dark" content="修改订单地址" placement="top">
                <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="showBox"></el-button>
              </el-tooltip>

              <!-- 展示物流信息按钮 -->
              <el-tooltip effect="dark" content="展示物流信息" placement="top">
                <el-button type="success" icon="el-icon-star-off" circle size="mini" @click="showProgressBox"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

            <!-- 页码显示 -->
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"></el-pagination>
        </div>
      </el-card>

      <!-- 修改订单地址对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="addressDialogVisible"
        width="50%"
        @close="addressDialogClosed">
        <el-form
          :model="addressForm"
          :rules="addressRules"
          ref="addressFormRef"
          label-width="100px">
          <el-form-item label="省市区/县" prop="address1">
            <!-- 选择商品分类的级联选择框-->
            <el-cascader
              :options="options"
              v-model="selectedOptions"
              :props="cateProps"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2" aria-placeholder="具体到门牌号码"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="addressDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
  </span>
      </el-dialog>

      <!-- 展示物流进度的对话框 -->
      <el-dialog
        title="物流进度"
        :visible.sync="progressDialogVisible"
        width="50%">
        <el-timeline :reverse="reverse">
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.ftime">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
    <el-button @click="progressDialogVisible = false">关闭</el-button>
  </span>
      </el-dialog>
    </div>
</template>

<script>
import { regionData } from 'element-china-area-data'

export default {
  name: 'Orders',
  data() {
    return {
      // 查询订单的条件
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单数据列表
      orderList: [],
      // 显示总页数
      total: 0,
      // 控制修改地址对话框的显示与隐藏
      addressDialogVisible: false,
      // 修改地址的数据对象
      addressForm: {
        // address1: [],
        address1: '',
        address2: ''
      },
      // 修改地址表单验证规则
      addressRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详情地址', trigger: 'blur' }
        ]
      },
      // 中国城市省份数据
      options: regionData,
      // 级联选择框选中的数据中的Id
      selectedOptions: [],
      // 级联选择框的规则对象
      cateProps: {
        // 鼠标移动到对应就会显示下一级
        expandTrigger: 'hover'
      },

      progressDialogVisible: false,

      // 物流信息数据
      progressInfo: [],
      reverse: false

    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    // 获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单数据失败')
      }
      console.log(res.data)
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.orderList)
    },
    // 监听 pagesize 改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    // 展示修改地址的对话框
    showBox() {
      this.addressDialogVisible = true
    },
    // 移除订单，并进行确认询问
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      if (res.meta.status !== 200) {
        return this.$message.error('查询物流信息失败！')
      }
      this.progressInfo = res.data
      console.log(res)
      console.log(this.progressInfo)
      this.progressDialogVisible = true
    },
    handleChange(value) {
      console.log(value)
    },
    // 监听修改地址对话框关闭事件
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
      this.selectedOptions = []
    }
  }
}
</script>

<style scoped lang="less">
.el-cascader{
  width: 100%;
}
.el-table{
  margin-top: 20px;
}
</style>
